package com.initialize.appapi.service;

import com.initialize.pub.dao.UserDAO;
import com.initialize.pub.mybatis.plus.entity.User;
import com.initialize.pub.mybatis.plus.mapper.UserMapper;
import com.initialize.pub.pojo.Result;
import com.initialize.pub.token.TokenService;
import org.springframework.stereotype.Service;

import java.util.LinkedHashMap;
import java.util.Map;
import java.util.Objects;

/**
 * @program: Initialization_Project
 * @description:
 * @author: Mr.Jane
 * @create: 2019-10-10 22:29
 **/
@Service
public class UserService {
    final
    UserDAO userDAO;

    final
    TokenService tokenService;

    final
    UserMapper userMapper;

    public UserService(UserDAO userDAO, TokenService tokenService, UserMapper userMapper) {
        this.userDAO = userDAO;
        this.tokenService = tokenService;
        this.userMapper = userMapper;
    }

    public Result login(Map<String, Object> stringObjectMap) {
        String login = stringObjectMap.get("login").toString();
        String psd = stringObjectMap.get("psd").toString();

        User user = userDAO.login(login, psd);
        Map<String, Object> userLoginInfoMap = new LinkedHashMap<>();

        if (!Objects.isNull(user)) {
            userLoginInfoMap.put("token", tokenService.getToken(user));
            return Result.success(userLoginInfoMap);
        } else {
            return Result.error("账号或密码错误");
        }
    }

    public Result UserInfo(int id) {
        return Result.success(userMapper.selectById(id));
    }
}
